package com.main.locatme;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.locatme.model.Enseigne;
import com.locatme.model.Langue;
import com.locatme.modelBD.LangueBD;
import com.locatme.modelBD.UtilisateurBD;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CreateEnseigneActivity extends Activity {


    private UtilisateurBD userBD;
    private LangueBD langueBD;
    private Langue langue;
    private Enseigne user;

    private static final String TAG_SUCCESS = "success";
    private JSONParser jsonParser = new JSONParser();

    private String  login;
    private String  password;
    private String  password2;
    private String  nomEnseigneString;
    private String  descEnseigneString;
    private String  houvertureString;
    private String hfermetureString;

    private  EditText loginEditText ;
    private  EditText passEditText1;
    private  EditText passEditText2;
    private  EditText nomEnseigne;
    private EditText descEnseigne;
    private EditText houverture;
    private EditText hfermeture;
    private  Spinner spinLangue;

    private static final String urlConnect = "http://locatme.alwaysdata.net/locateme/create_utilisateur.php";

    public Langue getLangue() {
        return langue;
    }

    public void setLangue(Langue langue) {
        this.langue = langue;
    }

    public UtilisateurBD getUserBD() {
        return userBD;
    }

    public void setUserBD(UtilisateurBD userBD) {
        this.userBD = userBD;
    }

    public LangueBD getLangueBD() {
        return langueBD;
    }

    public void setLangueBD(LangueBD langueBD) {
        this.langueBD = langueBD;
    }

    public Enseigne getUser() {
        return user;
    }

    public void setUser(Enseigne user) {
        this.user = user;
    }

    public EditText getLoginEditText() {
        return loginEditText;
    }

    public void setLoginEditText(EditText loginEditText) {
        this.loginEditText = loginEditText;
    }

    public EditText getPassEditText1() {
        return passEditText1;
    }

    public void setPassEditText1(EditText passEditText1) {
        this.passEditText1 = passEditText1;
    }

    public EditText getPassEditText2() {
        return passEditText2;
    }

    public void setPassEditText2(EditText passEditText2) {
        this.passEditText2 = passEditText2;
    }

    public EditText getNomEnseigne() {
        return nomEnseigne;
    }

    public void setNomEnseigne(EditText nomEnseigne) {
        this.nomEnseigne = nomEnseigne;
    }

    public EditText getDescEnseigne() {
        return descEnseigne;
    }

    public void setDescEnseigne(EditText descEnseigne) {
        this.descEnseigne = descEnseigne;
    }

    public EditText getHouverture() {
        return houverture;
    }

    public void setHouverture(EditText houverture) {
        this.houverture = houverture;
    }

    public EditText getHfermeture() {
        return hfermeture;
    }

    public void setHfermeture(EditText hfermeture) {
        this.hfermeture = hfermeture;
    }

    public Spinner getSpinLangue() {
        return spinLangue;
    }

    public void setSpinLangue(Spinner spinLangue) {
        this.spinLangue = spinLangue;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_enseigne);
        final Button createEnseigne = (Button) findViewById(R.id.btnregisterenseigne);
        nomEnseigne = (EditText) findViewById(R.id.editnomenseigne);
        loginEditText = (EditText) findViewById(R.id.editloginenseigne);
        passEditText1 = (EditText) findViewById(R.id.editmdpenseigne);
        passEditText2 = (EditText) findViewById(R.id.editverifmdpenseigne);
        descEnseigne = (EditText) findViewById(R.id.editdescritp);
        houverture = (EditText) findViewById(R.id.edithouvert);
        hfermeture = (EditText) findViewById(R.id.edithferm);
        spinLangue = (Spinner) findViewById(R.id.spinner1);



        createEnseigne.setOnClickListener(new OnClickListener() {


            @Override
            public void onClick(View v) {


                if (isNetworkConnected(getApplicationContext())) {// faire le test de la connexion si oui---
                    new CreateBusinessAccount().execute();
                    inserer(v);
                }else{
                    //sino -- message
                    Toast.makeText(CreateEnseigneActivity.this, "Veuillez vous connecter...", Toast.LENGTH_LONG).show();
                }
            }

        });

    }//Fin on create

    public static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        return (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected());
    }

    class CreateBusinessAccount extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
     super.onPreExecute();

        }

        @Override
        protected String doInBackground(String... strings) {


            nomEnseigne = (EditText) findViewById(R.id.editnomenseigne);
            loginEditText = (EditText) findViewById(R.id.editloginenseigne);
            passEditText1 = (EditText) findViewById(R.id.editmdpenseigne);
            passEditText2 = (EditText) findViewById(R.id.editverifmdpenseigne);
            descEnseigne = (EditText) findViewById(R.id.editdescritp);
            houverture =  (EditText) findViewById(R.id.edithouvert);
            hfermeture =  (EditText) findViewById(R.id.edithferm);
            spinLangue = (Spinner) findViewById(R.id.spinner1);


            nomEnseigneString =nomEnseigne.getText().toString();
            descEnseigneString= descEnseigne.getText().toString();
            houvertureString = houverture.getText().toString();
            hfermetureString = hfermeture.getText().toString();
            login = loginEditText.getText().toString();
            password = passEditText1.getText().toString();
            password2 = passEditText2.getText().toString();

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("login", login));
            params.add(new BasicNameValuePair("password", password));
            params.add(new BasicNameValuePair("password2", password2));
            params.add(new BasicNameValuePair("nomEnseigneString", nomEnseigneString));
            params.add(new BasicNameValuePair("descEnseigneString", descEnseigneString));
            params.add(new BasicNameValuePair("houvertureString", houvertureString));
            params.add(new BasicNameValuePair("hfermetureString", hfermetureString));
            JSONObject json = jsonParser.makeHttpRequest(urlConnect,
                    "POST", params);


            try {

                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    runOnUiThread(new Runnable() {
                        public void run() {

                            Toast.makeText(CreateEnseigneActivity.this," Enregistrement effectué ",Toast.LENGTH_LONG).show();
                        }
                    });

                    finish();
                }
                else {
                    runOnUiThread(new Runnable() {
                        public void run() {

                            Toast.makeText(CreateEnseigneActivity.this," Enregistrement échoué ",Toast.LENGTH_LONG).show();
                        }
                    });
                    finish();

                }

            } catch (JSONException e) {

                Log.d("Exeception de json : ", e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
        }
    }

    public void inserer(View v) {

        userBD = new UtilisateurBD(this);
        langueBD = new LangueBD(this);
        user = new Enseigne(loginEditText.getText().toString(), passEditText1.getText().toString(), nomEnseigne.getText().toString(), descEnseigne.getText().toString(), houverture.getText().toString(), hfermeture.getText().toString());

        userBD.open();
        langueBD.open();
        int idLangue = langueBD.recupIdLangue(spinLangue.getSelectedItem().toString());
        userBD.insertEnseigne(user);
        langueBD.insererLangueEnseigne(loginEditText.getText().toString(), idLangue);
        userBD.close();
        langueBD.close();

        Toast.makeText(this, "Vous venez de vous inscrire en tant que " + loginEditText.getText().toString(), Toast.LENGTH_SHORT).show();
        CreateEnseigneActivity.this.finish();
    }


}
